package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.BuildConfig;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.activity.JpApplicationWebViewTestActivity;
import com.jp.knowledge.activity.WebBrowserActivity;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.dialog.SelectDialog;
import com.jp.knowledge.logic.PersonLogic;
import com.jp.knowledge.service.LatestStateService;
import com.jp.knowledge.util.DataCleanManager;
import com.jp.knowledge.util.PackInfo;
import com.jp.knowledge.util.PushUtil;
import com.jp.knowledge.util.SharedUtil;
import com.kyleduo.switchbutton.SwitchButton;
import com.tencent.smtt.sdk.WebSettings;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by 528787067@qq.com on 2017/4/20.
 */

public class AppSettingActivity extends SlidingActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    @ViewInject(R.id.item_text_size)
    private LinearLayout itemTextSize;
    @ViewInject(R.id.desc_text_size)
    private TextView descTextSize;
    @ViewInject(R.id.item_clear_cache)
    private LinearLayout itemClearCache;
    @ViewInject(R.id.desc_clear_cache)
    private TextView descClearCache;
    @ViewInject(R.id.item_version)
    private LinearLayout itemVersion;
    @ViewInject(R.id.desc_version)
    private TextView descVersion;
    @ViewInject(R.id.exit_lly)
    private LinearLayout exitView;
    @ViewInject(R.id.exit_tv)
    private TextView exitBtn;
    @ViewInject(R.id.user_agreement)
    private TextView userAgreement;
    @ViewInject(R.id.call_us)
    private TextView callUs;

    @ViewInject(R.id.switch_receive_push)
    private SwitchButton receivePushSwitch;
    @ViewInject(R.id.switch_night_model)
    private SwitchButton nightModelSwitch;
    @ViewInject(R.id.switch_sound)
    private SwitchButton soundSwitch;
    @ViewInject(R.id.switch_vibrate)
    private SwitchButton vibrateSwitch;
    @ViewInject(R.id.switch_light)
    private SwitchButton lightSwitch;

    @ViewInject(R.id.goto_shop)
    private LinearLayout  gotoShop;

    private SelectDialog textSizeChooseDialog;
    private CommDialog clearCacheDialog;
    private CommDialog exitDialog;
    private CommDialog callDialog;

    private PushUtil pushUtil;

    private SharedPreferences sharedPreferences;

    @Override
    protected int getResourceId() {
        return R.layout.activity_app_setting;
    }

    @Override
    protected void init() {
        pushUtil = PushUtil.getInstance(this);
        sharedPreferences = getSharedPreferences("NewsDetail", MODE_PRIVATE);

        initTitleBar();
        initView();
        initDialog();
    }

    private void initTitleBar(){
        topName.setText("设置");
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);
    }

    private void initView(){
        itemTextSize.setOnClickListener(this);
        itemClearCache.setOnClickListener(this);
        itemVersion.setOnClickListener(this);
        exitBtn.setOnClickListener(this);
        userAgreement.setOnClickListener(this);
        callUs.setOnClickListener(this);

        gotoShop.setOnClickListener(new View.OnClickListener() {//去应用商店评分
            @Override
            public void onClick(View v) {
                WebBrowserActivity.openWebActivity(mContext, BuildConfig.CHANNEL_URL);
            }
        });

        setTextSizeItem();
        setCacheItem();
        setVersionItem();
        setExitItem();

        setPush();
    }

    private void initDialog(){
        textSizeChooseDialog = new SelectDialog(mContext);
        textSizeChooseDialog.setTitle("选择字体大小");
        textSizeChooseDialog.addItem("小");
        textSizeChooseDialog.addItem("中");
        textSizeChooseDialog.addItem("大");
        textSizeChooseDialog.addItem("特大");
        textSizeChooseDialog.setOnDialogItemClickListener(new SelectDialog.OnSelectDialogItemClickListener() {
            @Override
            public void onSelectItemClick(int position) {
                String text;
                switch (position){
                    case 0:     text = "小";;    break;
                    case 1:     text = "中";     break;
                    case 2:     text = "大";     break;
                    case 3:     text = "特大";    break;
                    default:    text = "中";
                }
                descTextSize.setText(text);
                sharedPreferences.edit().putInt("textSizeLevel", position).apply();

//                String text;
//                int size;
//                if(position == 0){
//                    text = "小";
//                    size = 14;
//                } else if(position == 2){
//                    text = "大";
//                    size = 18;
//                } else/* if(position == 1)*/{
//                    text = "中";
//                    size = 16;
//                }
//                SharedUtil.getInstance(mContext).saveCommFontSize(size);
//                SharedUtil.getInstance(mContext).saveCommFontSize(text);
//                descTextSize.setText(text);
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent(LocalBroadCast.COMM_CHANGE_SIZE));
            }
        });

        clearCacheDialog = new CommDialog(mContext);
        clearCacheDialog.setContent("是否清空应用缓存？");
        clearCacheDialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataCleanManager.cleanApplicationData(mContext);
                itemClearCache.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setCacheItem();
                    }
                }, 300);
            }
        });

        exitDialog = new CommDialog(mContext);
        exitDialog.setContent("是否退出当前账号？");
        exitDialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                application.clearLoginInfo();
                application.setUserData(null);
                new PersonLogic(mContext).deleteUserData();
                LocalBroadcastManager.getInstance(mContext)
                        .sendBroadcast(new Intent(LocalBroadCast.LOGOUT_SUCCESS_BROD));
                LatestStateService.clearAll();
                application.getApplicationWebView().setUserDataToH5(null);
                setExitItem();
                finish();
            }
        });

        callDialog = new CommDialog(mContext);
        callDialog.setContent("020-82513714");
        callDialog.setRight("呼叫");
        callDialog.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:020-82513714"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

    private void setTextSizeItem(){
        int textSize = sharedPreferences.getInt("textSizeLevel", 1);
        switch (textSize){
            case 0:     descTextSize.setText("小");      break;
            case 1:     descTextSize.setText("中");      break;
            case 2:     descTextSize.setText("大");      break;
            case 3:     descTextSize.setText("特大");    break;
            default:    descTextSize.setText("中");
        }
//        String text = SharedUtil.getInstance(mContext).getCommFontSizeText();
//        if(text == null)
//            descTextSize.setText("中");
//        else
//            descTextSize.setText(text);
    }

    private void setCacheItem(){
        try {
            descClearCache.setText(DataCleanManager.getCacheSize(getCacheDir()));
        } catch (Exception e) {
            descClearCache.setText("");
        }
    }

    private void setVersionItem(){
        String versionStr = PackInfo.getVersionName(mContext)
                /*+ " (" + getSharedPreferences("H5VersionData", MODE_PRIVATE).getLong("latest", 0) + ")"*/;
        descVersion.setText(versionStr);
    }

    private void setExitItem(){
        if(application.isLogin())
            exitView.setVisibility(View.VISIBLE);
        else
            exitView.setVisibility(View.GONE);
    }

    private void setPush(){
        receivePushSwitch.setChecked(pushUtil.getPushEnable());
        nightModelSwitch.setChecked(pushUtil.getNoDisturbMode());
        soundSwitch.setChecked(pushUtil.getNotificationPlaySound());
        vibrateSwitch.setChecked(pushUtil.getNotificationPlayVibrate());
        lightSwitch.setChecked(pushUtil.getNotificationPlayLights());

        receivePushSwitch.setOnCheckedChangeListener(this);
        nightModelSwitch.setOnCheckedChangeListener(this);
        soundSwitch.setOnCheckedChangeListener(this);
        vibrateSwitch.setOnCheckedChangeListener(this);
        lightSwitch.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.icon_left:
                finish();
                break;
            case R.id.item_text_size:
                textSizeChooseDialog.show();
                break;
            case R.id.item_clear_cache:
                clearCacheDialog.show();
                break;
            case R.id.item_version:

                break;
            case R.id.exit_tv:
                exitDialog.show();
                break;
            case R.id.user_agreement:
                startActivity(new Intent(mContext, HelpCatePageActivity.class)
                        .putExtra("pageId", "7c28fd89-2b55-50a5-163e-7474c0e996ad")
                        .putExtra("title", userAgreement.getText()));
                break;
            case R.id.call_us:
                callDialog.show();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switch_receive_push:
                pushUtil.setPushEnable(isChecked);
                nightModelSwitch.setEnabled(isChecked);
                soundSwitch.setEnabled(isChecked);
                vibrateSwitch.setEnabled(isChecked);
                lightSwitch.setEnabled(isChecked);
                break;
            case R.id.switch_night_model:
                pushUtil.setNoDisturbMode(isChecked);
                break;
            case R.id.switch_sound:
                pushUtil.setNotificationPlaySound(isChecked);
                break;
            case R.id.switch_vibrate:
                pushUtil.setNotificationPlayVibrate(isChecked);
                break;
            case R.id.switch_light:
                pushUtil.setNotificationPlayLights(isChecked);
                break;
        }
    }
}
